import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:palmf_hid/common/entities/data_base/my_database.dart';
import 'package:palmf_hid/common/services/Db.dart';

import 'add_pet_state.dart';

class AddPetLogic extends GetxController {
  final AddPetState state = AddPetState();
  String operateType = "add";
  final petNameFocusNode = FocusNode();
  final petNameTextEditingController = TextEditingController();
  final masterNameFocusNode = FocusNode();
  final masterNameTextEditingController = TextEditingController();
  final phoneFocusNode = FocusNode();
  final phoneTextEditingController = TextEditingController();
  final petWeightFocusNode = FocusNode();
  final petWeightTextEditingController = TextEditingController();
  final petYearFocusNode = FocusNode();
  final petYearTextEditingController = TextEditingController();
  final petMonthFocusNode = FocusNode();
  final petMonthTextEditingController = TextEditingController();
  PetInfo? petInfo;

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  void handleAddPet() async {
    if (operateType == "add") {
      await DbService.to.addPetInfo(PetInfoEntityCompanion.insert(
          orderNum: "",
          petName: state.petName,
          masterName: state.masterName,
          petYear: state.petYear,
          petMonth: state.petMonth,
          phone: state.phone,
          petWeight: state.petWeight,
          petSex: state.petSex,
          petType: state.petType,
          simpleType: "",
          barCode: "",
          otherOne: "",
          otherTwo: "",
          otherThree: ""));
      Get.back(result: true);
    } else {
      PetInfo petInfoL=PetInfo(
          id: petInfo!.id,
          orderNum: "",
          petName: petNameTextEditingController.text,
          masterName: masterNameTextEditingController.text,
          petYear: petYearTextEditingController.text,
          petMonth: petMonthTextEditingController.text,
          phone: phoneTextEditingController.text,
          petWeight: petWeightTextEditingController.text,
          petSex: state.petSex,
          petType: state.petType,
          simpleType: "",
          barCode: "",
          otherOne: "",
          otherTwo: "",
          otherThree: "");
      await DbService.to.updatePetInfo(petInfoL);
      Get.back(result: petInfoL);
    }
  }
  void handlePetInfo(PetInfo mPetInfo) {
    petInfo = mPetInfo;
    petNameTextEditingController.text = petInfo!.petName;
    masterNameTextEditingController.text = petInfo!.masterName;
    phoneTextEditingController.text = petInfo!.phone;
    petWeightTextEditingController.text = petInfo!.petWeight;
    petYearTextEditingController.text = petInfo!.petYear;
    petMonthTextEditingController.text = petInfo!.petMonth;
    state.petType = petInfo!.petType ;
    state.petSex = petInfo!.petSex ;
  }
}
